<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否可用" prop="isEnable">
        <el-select v-model="queryParams.isEnable" placeholder="请选择是否可用 " clearable size="small">
          <el-option
            v-for="dict in isEnableOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分成模式" prop="isEnable">
        <el-select v-model="queryParams.profitType" placeholder="请选择分成模式 " clearable size="small">
          <el-option
            v-for="dict in profitTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['agent:agent:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="agentList" >
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="是否可用" align="center" :formatter="isEnableFmt" prop="isEnable" />
      <el-table-column label="分成模式" align="center" :formatter="profitTypeFmt" prop="profitType" />
      <el-table-column label="分成金额" align="center" prop="agentFee" />
      <el-table-column label="分成比例" align="center" prop="agentRadio" />
      <el-table-column label="关联会员" align="center" prop="memberMobile" />
      <el-table-column label="创建时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown" >
              <el-dropdown-item v-hasPermi="['agent:agent:edit']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >修改</el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['agent:agent:bindMember']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-s-custom"
                  @click="handleBind(scope.row)"
                >绑定会员</el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['agent:agent:syncMember']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-refresh"
                  @click="handleSync(scope.row)"
                >同步会员</el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['agent:agent:resetPwd']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-key"
                  @click="handleResetPwd(scope.row)"
                >重置密码</el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['station:station:list']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-s-help"
                  @click="handleStationList(scope.row)"

                >检测站列表
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-s-grid"
                  @click="handleOrder(scope.row)"
                  v-hasPermi="['order:order:list']"
                >订单列表
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合作商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="55%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号" prop="mobile">
              <el-input v-model="form.mobile" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="昵称" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入昵称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
              <el-form-item label="是否可用" prop="isEnable">
                <el-select v-model="form.isEnable" placeholder="请选择是否可用 " clearable size="small">
                  <el-option
                    v-for="dict in isEnableOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="parseInt(dict.dictValue)"
                  />
                </el-select>
              </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分成模式" prop="profitType">
              <el-select v-model="form.profitType" placeholder="请选择分成模式 " clearable size="small">
                <el-option
                  v-for="dict in profitTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="分成金额" prop="agentFee">
              <el-input-number v-model="form.agentFee" :precision="2"  :min="0.01" :max="200" placeholder="请输入分成金额"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分成比例" prop="agentRadio">
              <el-input-number v-model="form.agentRadio" :precision="2"  :min="0.01" :max="0.99" laceholder="请输入分成比例"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--绑定会员-->
    <el-dialog title="绑定会员" :visible.sync="openBind" width="50%">
      <el-form ref="formBind" :model="formBind" :rules="rules" label-width="100px">
        <el-form-item label="会员手机号" prop="mobile">
          <el-input v-model="formBind.mobile" placeholder="请输入会员手机号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormBind">确 定</el-button>
        <el-button @click="openBind = false">取 消</el-button>
      </div>
    </el-dialog>
    <!--检测站列表-->
    <el-dialog title="检测站列表" :visible.sync="openStation" v-if="openStation" width="80%" append-to-body>
      <station :agentId="agentId"></station>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openStation = false">关闭</el-button>
      </div>
    </el-dialog>
    <!--订单列表-->
    <el-dialog title="订单列表" :visible.sync="orderopen" width="80%" v-if="orderopen" append-to-body>
      <order-list :stationAgentId="agentId"></order-list>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="orderopen = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAgent, getAgent, resetAgentPwd, addAgent, updateAgent, bindMember,syncMember } from "@/api/agent/agent";
import orderList from '../../order/order/index'
import Station from './station'
export default {
  name: "Agent",
  components: {orderList,Station},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 合作商表格数据
      agentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      orderopen: false,
      openStation: false,
      openBind: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isEnable: undefined,
        openId: undefined,
        profitType: undefined,
        mobile: undefined,
        nickName: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      isEnableOptions:[],
      profitTypeOptions:[],
      agentTypeOptions:[],
      formBind: {},
      agentId: undefined
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_is_enable").then(response => {
      this.isEnableOptions = response.data;
    });
    this.getDicts("agent_profit_type").then(response => {
      this.profitTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询合作商列表 */
    getList() {
      this.loading = true;
      listAgent(this.queryParams).then(response => {
        this.agentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gmtTime: undefined,
        isEnable: undefined,
        openId: undefined,
        parentId: undefined,
        agentFee: undefined,
        agentRadio: undefined,
        profitType: undefined,
        password: undefined,
        mobile: undefined,
        nickName: undefined,
        id: undefined
      };
      this.resetForm("form");
    },
    resetFormBind(id) {
      this.formBind = {
        id: id,
        mobile: undefined,
      };
      this.resetForm("formBind");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合作商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAgent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合作商";
      });
    },
    /** 检测站列表按钮操作 */
    handleStationList(row) {
      this.openStation = true;
      this.agentId = row.id;
    },
    /** 订单列表按钮操作 */
    handleOrder(row) {
      this.orderopen = true;
      this.agentId = row.id;
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateAgent(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addAgent(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    submitFormBind: function() {
      bindMember(this.formBind).then(response => {
        if (response.code === 200) {
          this.msgSuccess("绑定成功");
          this.openBind = false;
          this.getList();
        } else {
          this.msgError(response.msg);
        }
      });
    },
    /** 重置密码按钮操作 */
    handleResetPwd(row) {
      this.$prompt('请输入"' + row.nickName + '"的新密码', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      }).then(({ value }) => {
        resetAgentPwd({
          id: row.id,
          password: value
        }).then(response => {
          if (response.code === 200) {
            this.msgSuccess("修改成功，新密码是：" + value);
          } else {
            this.msgError(response.msg);
          }
        });
      }).catch(() => {});
    },
    /** 绑定按钮操作 */
    handleBind(row) {
      this.resetFormBind(row.id);
      this.openBind = true;
    },
    /** 同步会员信息 */
    handleSync(row) {
      this.$confirm('是否确认同步最新会员信息?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return syncMember(row.id);
      }).then(() => {
        this.getList();
        this.msgSuccess("同步成功");
      }).catch(function () {
      });
    },
    isEnableFmt(row, column) {
      return this.selectDictLabel(this.isEnableOptions, row.isEnable);
    },
    profitTypeFmt(row, column) {
      return this.selectDictLabel(this.profitTypeOptions, row.profitType);
    }
  }
};
</script>
